
package com.xin.di.uav.applet.mapper;

import com.xin.di.uav.common.response.IdAndNameResp;
import com.xin.di.uav.operate.po.ServiceAreaPO;
import com.xin.di.uav.operate.po.SysRolePO;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface RoleMapper {

    // 动态字段更新（只更新非空字段）
    @Update({
            "<script>",
            "UPDATE service_areas ",
            "<set>",
            "    <if test='serviceRegion != null'>service_region = #{serviceRegion},</if>",
            "    <if test='latitude != null'>latitude = #{latitude},</if>",
            "    <if test='longitude != null'>longitude = #{longitude},</if>",
            "    <if test='detailedAddress != null'>detailed_address = #{detailedAddress},</if>",
            "    <if test='personInCharge != null'>person_in_charge = #{personInCharge},</if>",
            "    <if test='contactNumber != null'>contact_number = #{contactNumber},</if>",
            "    <if test='status != null'>status = #{status},</if>",
            "    updated_at = NOW()",

            "</set>",
            "WHERE id = #{id}",
            "</script>"
    })
    int updateById(ServiceAreaPO serviceArea);

    @Select({"<script>",
            "select id, service_region, latitude, longitude, detailed_address, person_in_charge, contact_number,\n" +
                    "                           created_at, updated_at, status\n" +
                    "from service_areas where id =#{id} ",
            "</script>"})
    ServiceAreaPO selectById(String id);




    @Select({"<script>",
            "select count(1) \n" +
                    "from sys_role where 1=1  ",
            "    <if test='roleName != null and roleName !=\"\"  '> and  role_name like CONCAT('%', #{roleName}, '%') </if>",
            "</script>"})
    int count(String roleName);


    @Select({"<script>",
            "select id, role_name, role_code, status, sort, create_time, update_time, remark \n" +
                    " from sys_role where  1=1 " +
                    "    <if test='roleName != null and roleName !=\"\"  '> and  role_name like CONCAT('%', #{roleName}, '%') </if>",
            "order by create_time desc   limit #{offset},#{pageSize} ",
            "</script>"})
    List<SysRolePO> selectList(@Param("roleName") String roleName,@Param("offset") int offset,@Param("pageSize") int pageSize);


    @Select({"<script>",
            "select id,role_name as name from sys_role where status=1" +
                    "    <if test='name != null and name !=\"\"  '> and  role_name like CONCAT('%', #{name}, '%') </if>",
                    " order by create_time desc ",
            "</script>"})
    List<IdAndNameResp> selIdAndName(String name);


    @Select({"<script>",
            "select id, role_name, role_code, status, sort, create_time, update_time, remark \n" +
                    " from sys_role where  id in  " +
                    "       <foreach item=\"id\" collection=\"ids\" open=\"(\" separator=\",\" close=\")\">\n" +
                    "            #{id}\n" +
                    "        </foreach>" +
            "</script>"})
    List<SysRolePO> selRoles(@Param("ids") List<String> ids);


    @Select({"<script>",
            "select id,service_region as name \n" +
                    " from service_areas where  1=1 " +
                    "    <if test='serviceRegion != null and serviceRegion !=\"\"  '> and  service_region like CONCAT('%', #{serviceRegion}, '%') </if>",
            "order by created_at desc   ",
            "</script>"})
    List<IdAndNameResp> searchList(String serviceRegion);


    @Insert({"<script>",
            "insert into sys_role (id, role_name, remark)\n" +
                    "values (#{id}, #{roleName}, #{remark})",
            "</script>"})
    int insert(SysRolePO sysRolePO);


    @Delete({"<script>",
            "delete from sys_role where id=#{id}",
            "</script>"})
    int deleteById(String id);
}
